package com.share.common.enums;

import lombok.Getter;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * HTTP状态码枚举（中文版）
 */
@Getter
public enum HttpStatusEnum {
    // 1xx 信息响应
    CONTINUE(100, "继续"),
    SWITCHING_PROTOCOLS(101, "切换协议"),
    PROCESSING(102, "处理中"),
    EARLY_HINTS(103, "早期提示"),

    // 2xx 成功
    SUCCESS(200, "请求成功"),
    CREATED(201, "已创建"),
    ACCEPTED(202, "已接受"),
    NON_AUTHORITATIVE_INFORMATION(203, "非授权信息"),
    NO_CONTENT(204, "无内容"),
    RESET_CONTENT(205, "重置内容"),
    PARTIAL_CONTENT(206, "部分内容"),
    MULTI_STATUS(207, "多状态"),
    ALREADY_REPORTED(208, "已报告"),
    IM_USED(226, "IM已使用"),

    // 3xx 重定向
    MULTIPLE_CHOICES(300, "多种选择"),
    MOVED_PERMANENTLY(301, "永久移动"),
    FOUND(302, "临时移动"),
    SEE_OTHER(303, "查看其他位置"),
    NOT_MODIFIED(304, "未修改"),
    USE_PROXY(305, "使用代理"),
    TEMPORARY_REDIRECT(307, "临时重定向"),
    PERMANENT_REDIRECT(308, "永久重定向"),

    // 4xx 客户端错误
    BAD_REQUEST(400, "错误请求"),
    UNAUTHORIZED(401, "未授权"),
    PAYMENT_REQUIRED(402, "需要付款"),
    FORBIDDEN(403, "禁止访问"),
    NOT_FOUND(404, "未找到"),
    METHOD_NOT_ALLOWED(405, "方法不允许"),
    NOT_ACCEPTABLE(406, "不可接受"),
    PROXY_AUTHENTICATION_REQUIRED(407, "需要代理认证"),
    REQUEST_TIMEOUT(408, "请求超时"),
    CONFLICT(409, "冲突"),
    GONE(410, "已删除"),
    LENGTH_REQUIRED(411, "需要有效长度"),
    PRECONDITION_FAILED(412, "预处理失败"),
    PAYLOAD_TOO_LARGE(413, "请求实体过大"),
    URI_TOO_LONG(414, "URI过长"),
    UNSUPPORTED_MEDIA_TYPE(415, "不支持的媒体类型"),
    RANGE_NOT_SATISFIABLE(416, "请求范围不符合要求"),
    EXPECTATION_FAILED(417, "预期失败"),
    I_AM_A_TEAPOT(418, "我是一个茶壶"),
    MISDIRECTED_REQUEST(421, "错误定向请求"),
    UNPROCESSABLE_ENTITY(422, "不可处理的实体"),
    LOCKED(423, "已锁定"),
    FAILED_DEPENDENCY(424, "失败的依赖"),
    TOO_EARLY(425, "过早"),
    UPGRADE_REQUIRED(426, "需要升级"),
    PRECONDITION_REQUIRED(428, "需要预处理"),
    TOO_MANY_REQUESTS(429, "请求过多"),
    REQUEST_HEADER_FIELDS_TOO_LARGE(431, "请求头字段过大"),
    UNAVAILABLE_FOR_LEGAL_REASONS(451, "因法律原因不可用"),

    // 5xx 服务器错误
    INTERNAL_SERVER_ERROR(500, "服务器内部错误"),
    NOT_IMPLEMENTED(501, "未实现"),
    BAD_GATEWAY(502, "错误网关"),
    SERVICE_UNAVAILABLE(503, "服务不可用"),
    GATEWAY_TIMEOUT(504, "网关超时"),
    HTTP_VERSION_NOT_SUPPORTED(505, "HTTP版本不受支持"),
    VARIANT_ALSO_NEGOTIATES(506, "变体也可协商"),
    INSUFFICIENT_STORAGE(507, "存储空间不足"),
    LOOP_DETECTED(508, "检测到循环"),
    NOT_EXTENDED(510, "未扩展"),
    NETWORK_AUTHENTICATION_REQUIRED(511, "需要网络认证");

    private final Integer code;
    private final String message;

    private static final Map<Integer, HttpStatusEnum> CODE_TO_ENUM = 
        Arrays.stream(values())
              .collect(Collectors.toMap(HttpStatusEnum::getCode, e -> e));

    HttpStatusEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    /**
     * 根据状态码获取枚举实例
     * @param code HTTP状态码
     * @return 对应的枚举实例，找不到时返回null
     */
    public static HttpStatusEnum getByCode(Integer code) {
        if (code == null) {
            return null;
        }
        return CODE_TO_ENUM.get(code);
    }

    /**
     * 判断状态码是否属于信息响应类(1xx)
     * @return 如果是1xx状态码返回true，否则false
     */
    public boolean is1xxInformational() {
        return code >= 100 && code < 200;
    }

    /**
     * 判断状态码是否属于成功类(2xx)
     * @return 如果是2xx状态码返回true，否则false
     */
    public boolean is2xxSuccessful() {
        return code >= 200 && code < 300;
    }

    /**
     * 判断状态码是否属于重定向类(3xx)
     * @return 如果是3xx状态码返回true，否则false
     */
    public boolean is3xxRedirection() {
        return code >= 300 && code < 400;
    }

    /**
     * 判断状态码是否属于客户端错误类(4xx)
     * @return 如果是4xx状态码返回true，否则false
     */
    public boolean is4xxClientError() {
        return code >= 400 && code < 500;
    }

    /**
     * 判断状态码是否属于服务器错误类(5xx)
     * @return 如果是5xx状态码返回true，否则false
     */
    public boolean is5xxServerError() {
        return code >= 500 && code < 600;
    }

    @Override
    public String toString() {
        return code + " " + message;
    }
}